home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / CHEM.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  92 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5. /* Modified for Quintus Prolog by Andreas Siebert */
  6.  
  7. /* CHEM.PL */
  8. /* Molecular query predicates and examples */
  9.  
  10. /* Description of 3-chloro-toluene molecule:
  11.  
  12.                       H1      Ch
  13.                        \     /
  14.                   H2    C1--C2
  15.                   |    //    \\
  16.               H3--C3--C4      C5--H4
  17.                   |    \     /
  18.                   H5    C6==C7
  19.                        /      \
  20.                       H6      H7
  21.  
  22. */
  23.  
  24. atom_specs(h1,hydrogen,[c1]).
  25. atom_specs(h2,hydrogen,[c3]).
  26. atom_specs(h3,hydrogen,[c3]).
  27. atom_specs(h4,hydrogen,[c5]).
  28. atom_specs(h5,hydrogen,[c3]).
  29. atom_specs(h6,hydrogen,[c6]).
  30. atom_specs(h7,hydrogen,[c7]).
  31. atom_specs(c1,carbon,[c2,c4,h1]).
  32. atom_specs(c2,carbon,[c1,c5,ch]).
  33. atom_specs(c3,carbon,[c4,h2,h3,h5]).
  34. atom_specs(c4,carbon,[c1,c3,c6]).
  35. atom_specs(c5,carbon,[c2,c7,h4]).
  36. atom_specs(c6,carbon,[c4,c7,h6]).
  37. atom_specs(c7,carbon,[c5,c6,h7]).
  38. atom_specs(ch,chlorine,[c2]).
  39.  
  40.  
  41. /* Predicates to find bonds and identify elements */
  42.  
  43. bonded(A1,A2) :-
  44.      atom_specs(A1,_,Neighbors),
  45.      member(A2,Neighbors).
  46.  
  47. member(X,[X|_]).
  48. member(X,[_|Y]) :- member(X,Y).
  49.  
  50. element(A1,Element) :- atom_specs(A1,Element,_).
  51.  
  52.  
  53. /* Predicates to identify particular substructures */
  54.  
  55. methyl(C) :-
  56.      element(C,carbon),
  57.      bonded(C,H1), element(H1,hydrogen),
  58.      bonded(C,H2), element(H2,hydrogen), H1 \== H2,
  59.      bonded(C,H3), element(H3,hydrogen), H3 \== H1,
  60.           H3 \== H2.
  61.  
  62. six_membered_carbon_ring([A1,A2,A3,A4,A5,A6]) :-
  63.      element(A1,carbon), bonded(A1,A2),
  64.      element(A2,carbon), bonded(A2,A3), A1 \== A3,
  65.      element(A3,carbon), bonded(A3,A4),
  66.           \+ member(A4,[A1,A2,A3]),
  67.      element(A4,carbon), bonded(A4,A5),
  68.           \+ member(A5,[A1,A2,A3,A4]),
  69.      element(A5,carbon), bonded(A5,A6),
  70.      element(A6,carbon), bonded(A6,A1),
  71.           \+ member(A6,[A1,A2,A3,A4,A5]).
  72.  
  73. meth_carbon_ring([C|Ring]) :-
  74.      six_membered_carbon_ring(Ring),
  75.      member(A,Ring), bonded(A,C), methyl(C).
  76.  
  77. hydroxide(O) :- element(O,oxygen),
  78.                      bonded(O,H),
  79.                 element(H,hydrogen).
  80.  
  81.  
  82.  
  83. /* Demonstrations */
  84.  
  85. demo1 :-    write('Searching for a methyl group...'),nl,
  86.             methyl(X),
  87.             write('Found one centered on atom: '),write(X),nl.
  88.  
  89. demo2 :-    write('Searching for a six-membered carbon ring...'),nl,
  90.             six_membered_carbon_ring(List),
  91.             write('Found one containing atoms: '),write(List),nl.
  92.